Unity连接本地数据库sqlite
首先要创建一个sqlite的数据库,记住文件地址,拷贝到Assets目录下,创建的数据库文件后缀为.sqlite。具体创建方法百度sqlite
然后百度Mono.Data.Sqlite,这是一个dll文件,下载下来,同样需要放到Assets目录下
接下来创建c#脚本,
using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
using System.IO;
public class SQLiteDemo : MonoBehaviour {
string sqlQuery = "create table unity (unity1 int,unity2 int)";
string sqlQuery = "select * from unity";
// Use this for initialization
void Start () {
string connectionString = "Data Source = " + Application.dataPath + "/test.sqlite";
SqliteConnection dbConnection;
dbConnection = new SqliteConnection (connectionString);
dbConnection.Open ();
SqliteCommand dbCommand;
dbCommand = dbConnection.CreateCommand();
dbCommand.CommandText = sqlQuery;
dbCommand.ExecuteNonQuery ();
SqliteDataReader dbReader;
dbReader = dbCommand.ExecuteReader ();
while(dbReader.Read())
{
for(int i = 0; i< dbReader.FieldCount; i++)
{
Debug.Log(dbReader.GetName(i));
Debug.Log(dbReader.GetValue(i));
}
}
}
}
拖到任意物体上,运行。